#include <WiFi.h>
#include <PubSubClient.h>
const char *ssid = "AABBCC";
const char *password = "12345678";
const char *mqttServer = "iot-06z00j2prt9lr1n.mqtt.iothub.aliyuncs.com";//"d97d64a5db.st1.iotda-device.cn-east-3.myhuaweicloud.com";
const int mqttPort = 1883;
// 以下3个参数可以由HMACSHA256算法生成，为硬件通过MQTT协议接入华为云IoT平台的鉴权依据
const char *clientId = "k0o2vTxYhy2.devicename|securemode=2,signmethod=hmacsha256,timestamp=1719818469676|";//"66814f2905b438773bf73d09esp32_0_0_2024063013";
const char *mqttUser = "devicename&k0o2vTxYhy2";//"66814f2905b438773bf73d09esp32";
const char *mqttPassword = "b6bc3d6a5bddb464061002dfccb558e9a0ba48e8e8ffe1b6bbd03120f3912295";//"de19f93fe0d0ccd57a80906ec4a8a668b9d22ef790667ad56c28e20fde83c42d";
const char *pubtopic = "/k0o2vTxYhy2/devicename/user/SHUJU";
const char *subtopic = "/k0o2vTxYhy2/devicename/user/KONGZHI";
#define abc "{\"fuyang\":24,\"fangun\":33,\"zuoshang\":25,\"zuoxia\":40,\"youshang\":26,\"youxia\":40}"
#define def "{\"fuyang\":35,\"fangun\":15,\"zuoshang\":33,\"zuoxia\":180,\"youshang\":50,\"youxia\":60}"
#define xyz "{\"fuyang\":28,\"fangun\":22,\"zuoshang\":33,\"zuoxia\":180,\"youshang\":50,\"youxia\":60}"
// 参考上报格式：{"services":[{"service_id":"Dev_data","properties":{"temp": 39}}]}
// 设备属性上报

WiFiClient espClient; // ESP32WiFi模型定义
PubSubClient client(espClient);

char receivechar;
char receivebuffer[200];
byte point;//STM32包指针

void callback(char *topic,byte *payload,unsigned int length)
{
  //Serial.print("订阅：");
  //Serial.println(topic);
  //Serial.print("内容:");
  //for(int i=0;i<length;i++){
  //  Serial.print((char)payload[i]);  
  //}
  //Serial.println("");
  if(payload[13]=='1')
  {
    Serial2.print("@mo1walkw#");
  }
  else if (payload[13]=='2')
  {
    Serial2.print("@mo1stand#");
    /* code */
  }
  else if (payload[13]=='3')
  {
    Serial2.print("@mo1sitsi#");
    /* code */
  }
  else if (payload[13]=='4')
  {
    Serial2.print("@mo2walkw#");
    /* code */
  }
  else if (payload[13]=='5')
  {
    Serial2.print("@mo2stand#");
    /* code */
  }
  else if (payload[13]=='6')
  {
    Serial2.print("@mo2sitsi#");
    /* code */
  }
  else if (payload[13]=='7')
  {
    Serial2.print("@mo3walkw#");
    /* code */
  }
  else if (payload[13]=='8')
  {
    Serial2.print("@mo3stand#");
    /* code */
  }
  else
  {    
    Serial2.print("@mo3sitsi#");
  }
}

void MQTT_Init()
{
  // WiFi网络连接部分
  WiFi.begin(ssid, password); // 开启ESP32的WiFi
  while (WiFi.status() != WL_CONNECTED)
  { // ESP尝试连接到WiFi网络
    delay(2000);
    //Serial2.println("连接WiFi...");
  }
  //Serial2.println("连接成功");

  // MQTT服务器连接部分
  client.setServer(mqttServer, mqttPort); // 设置连接到MQTT服务器的参数

  client.setKeepAlive(3600); // 设置心跳时间

  while (!client.connected())
  { // 尝试与MQTT服务器建立连接
    //Serial2.println("连接MQTT...");

    if (client.connect(clientId, mqttUser, mqttPassword))
    {

      //Serial2.println("连接成功");
    }
    else
    {

      //Serial2.print("失败状态： ");
      //Serial2.print(client.state());
      delay(3000);
    }
  }

  // 接受平台下发内容的初始化
  client.subscribe(subtopic);
  client.setCallback(callback); //可以接受任何平台下发的内容
}


void setup()
{
  Serial2.begin(115200);
  MQTT_Init();
}

void loop() {
  //over:
  client.loop();
  //if(Serial2.available())
  //{
  //  if(receivechar=='$')
  //  {
  //    client.publish(pubtopic,receivebuffer,point);
  //    point=0;
  //    goto over;
  //  }
  //  receivechar=Serial2.read();
  //  receivebuffer[point]=receivechar;
  //  point++;
  //}
  client.publish(pubtopic,abc);
  delay(1500);
  client.publish(pubtopic,def);
  delay(1500);
  client.publish(pubtopic,xyz);
  delay(1500);
}